# /bin/bash

MYSQL_PASSWORD='@worrylin'
MYSQL_CHARSET='utf8mb4'
MYSQL_COLLATE='utf8mb4_unicode_ci'
GOGS_DOMAIN='git.ilosty.com'
DB_GOGS_USER='gogs'
DB_GOGS_PWD='@Gogs001'
GOGS_ADMIN_USER='worry'
GOGS_ADMIN_PWD='ilosty910313'


useradd www
mkdir -p /home/www
chown www:www -R /home/www
yum -y install epel-release gcc gettext autoconf libtool automake make pcre-devel asciidoc xmlto c-ares-devel libev-devel libsodium-devel mbedtls-devel

# gogs setup
wget -O /etc/yum.repos.d/gogs.repo \
  https://dl.packager.io/srv/gogs/gogs/main/installer/el/7.repo
yum -y install gogs

# install remi repos
wget https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm

# install last version redis with remi-repo
yum --enablerepo=remi install redis -y

# install nginx
yum install -y nginx

# mysql setup
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
rpm -Uvh mysql80-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"

# you can choose the install version
#yum-config-manager --enable mysql57-community
yum-config-manager --enable mysql80-community

# centos8 need run this command to disable default mysql version
# yum module disable mysql

# install mysql client and server
yum install -y mysql-community-client mysql-community-server

# if your password is weekly you need set password policy to LOW
#    - LOW Length >= 8 characters.
#    - MEDIUM Length >= 8, numeric, mixed case, and special characters.
#    - STRONG Length >= 8, numeric, mixed case, special characters and dictionary file.
echo "validate_password.policy=LOW" >> /etc/my.cnf

# start mysql server, only after first start server, print default password in mysqld.log
systemctl start mysqld

# get default password
MYSQL_DEFAULT_PASSWORD=`grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'`

# reset root password

# new version need --connect-expired-password
# MYSQL_PWD=$MYSQL_DEFAULT_PASSWORD mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';FLUSH PRIVILEGS;"
MYSQL_PWD=$MYSQL_DEFAULT_PASSWORD mysql -u root --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';FLUSH PRIVILEGS;"

# create database gogs
MYSQL_PWD=$MYSQL_PASSWORD mysql -u root -e "create database gogs default character set $MYSQL_CHARSET collate $MYSQL_COLLATE;"
# create gogs user
MYSQL_PWD=$MYSQL_PASSWORD mysql -u root -e "CREATE USER IF NOT EXISTS ${DB_GOGS_USER}@localhost IDENTIFIED BY '${DB_GOGS_PWD}';GRANT ALL ON gogs.* to gogs@localhost;FLUSH PRIVILEGES;"

# create nginx gogs conf file
cat > /etc/nginx/conf.d/$GOGS_DOMAIN.conf <<EOF
server {
  listen          80;
  server_name     ${GOGS_DOMAIN};
  location / {
    proxy_pass      http://localhost:6000;
  }
}
EOF
cat > /etc/nginx/php-fpm.conf <<EOF
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
        fastcgi_param  PATH_INFO  \$fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  \$document_root\$fastcgi_path_info;
        include        fastcgi_params;
    }
EOF
cat > /etc/nginx/cros.conf <<EOF
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 204;
}
EOF

# set nginx run user
sed -i -E "s/user \w+/user www/" /etc/nginx/nginx.conf


# install php
yum-config-manager --enable remi-php72
yum install -y php php-cli php-fpm php-bcmath php-curl php-gd php-igbinary php-imagick php-json php-mbstring php-mysql php-opcache php-readline php-redis php-xdebug php-xml php-mcrypt php-fileinfo php-ldap php-odbc php-pear php-xmlrpc php-soap php-mysqlnd php-zip

# set php-fpm run user
sed -i -E "s/^\s*user\s*=\s*\w+/user = www/" /etc/php-fpm.d/www.conf
sed -i -E "s/^\s*group\s*=\s*\w+/group = www/" /etc/php-fpm.d/www.conf

systemctl start php-fpm
systemctl start nginx
systemctl start redis
systemctl start gogs

systemctl enable php-fpm
systemctl enable nginx
systemctl enable mysqld
systemctl enable redis
systemctl enable gogs


# TODO: wait to do list
# visit link http://${GOGS_DOMAIN}/install to init gogs data